/*
  * Copyright (c) 2015 by Chances.
  * $HeadURL$:
  * $Author$:
  * $Date$:
  * $Rev$:
  */
package chances.epg.data.taglib;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;

import chances.epg.commons.utils.URLUtils;
import chances.epg.data.taglib.page.PageInfo;
import chances.epg.entity.content.Schedule;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.context.AccessContextUtils;

/**
 * @author snvi
 * 2015-6-24
 *
 */
public class ScheduleTag extends QueryListTag {
    
    private static final long serialVersionUID = 2014731478565300098L;
    private String channelCode;
    private String startTime;
    private String endTime;
    private String type;
    
    private static final String TYPE_TIME = "time";    //一段时间内的节目单
    private static final String TYPE_BACK = "back";    //回看
    private static final String TYPE_LIVE = "live";    //直播
    private static final String TYPE_FUTURE = "future";//预报
    
    private int size = 10;//条目数

    @Override
    public int doEndTag() throws JspException {
        if(StringUtils.isNotBlank(this.getPageName())){
            this.queryItemByPage();
        }else{
            this.queryItemBySize();
        }
        return super.doEndTag();
    }
    
    private void queryItemBySize() {
        Map<String,Object> paramsMap = new HashMap<String,Object>();
        paramsMap.put("channelCode", this.channelCode);
        List<Schedule> result = null;
        if(StringUtils.isNotBlank(this.type)){
            if(this.type.equals(TYPE_TIME)){
                if(StringUtils.isBlank(this.startTime)||StringUtils.isBlank(this.endTime)){
                    return;
                }
                paramsMap.put("cStartDataTime", this.startTime);
                paramsMap.put("cEndDataTime", this.endTime);
                result = this.getChannelService().getScheduleByTime(paramsMap,this.start,this.size);
            }else if(this.type.equals(TYPE_BACK)){
                result = this.getChannelService().getPlayedSchedule(paramsMap,this.start,this.size);
            }else if(this.type.equals(TYPE_LIVE)){
                result = this.getChannelService().getPlayingSchedule(paramsMap,this.start,this.size);
            }else if(this.type.equals(TYPE_FUTURE)){
                result = this.getChannelService().getFutureSchedule(paramsMap,this.start,this.size);
            }
            this.setSchedules(result);
        }
    }
    
    private void queryItemByPage() {
        PageInfo pageInfo = this.createPageInfo(this.size);
        Map<String,Object> paramsMap = new HashMap<String,Object>();
        paramsMap.put("channelCode", this.channelCode);
        List<Schedule> result = null;       
        if(StringUtils.isNotBlank(this.type)){
            if(this.type.equals(TYPE_TIME)){
                if(StringUtils.isBlank(this.startTime)||StringUtils.isBlank(this.endTime)){
                    return;
                }
                paramsMap.put("cStartDataTime", this.startTime);
                paramsMap.put("cEndDataTime", this.endTime);
                result = this.getChannelService().getScheduleByTime(paramsMap,this.start,pageInfo.getPageBean());
            }else if(this.type.equals(TYPE_BACK)){
                result = this.getChannelService().getPlayedSchedule(paramsMap,this.start,pageInfo.getPageBean());
            }else if(this.type.equals(TYPE_LIVE)){
                result = this.getChannelService().getPlayingSchedule(paramsMap,this.start,pageInfo.getPageBean());
            }else if(this.type.equals(TYPE_FUTURE)){
                result = this.getChannelService().getFutureSchedule(paramsMap,this.start,pageInfo.getPageBean());
            }
            this.setPageInfo(pageInfo);
            this.setSchedules(result);
        }
    }
    
    private void setSchedules(List<Schedule> result) {
        int index = 0;
        AccessContext context = AccessContextUtils.getAccessContext(pageContext);
        if (null != context) {
            for (Schedule schedule : result) {
                index++;
                String url = navigateUrlFactory.generateURL(context, schedule);
                url = URLUtils.appendParam(url, "pi", index + "");
                schedule.setUrl(url);
            }
        }
        this.setCollectionResult(result);
    }

    public String getChannelCode() {
        return channelCode;
    }

    public void setChannelCode(String channelCode) {
        this.channelCode = channelCode;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}
